package w20rest.java;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.junit.Test;
import org.springframework.web.client.support.RestGatewaySupport;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.LinkedList;
import java.util.List;

public class BookstoreClient extends RestGatewaySupport {

    public static final String AP = "http://localhost:8888/";

    @Test
    public void test() {
        getRestTemplate().getMessageConverters().add(new org.springframework.http.converter.json.MappingJackson2HttpMessageConverter());

        testGetAllBooks();
//        testGetBookWithId6();
//        testGetUnexistingBookWithId5();
//        testGetBookWithWithPublisherId5();
        testAddBook();
        testGetAllBooks();

    }

    private void testGetUnexistingBookWithId5() {
        String uri = AP;
        uri += "books/5";

        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
        final String uriString = uriComponentsBuilder.build().encode().toUriString();
        System.out.println(uriString);
        String response = getRestTemplate().getForObject(uriString, String.class);
        System.out.println(response);
    }

    private void testAddBook() {
        String uri = AP;
        uri += "books";

        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
        final String uriString = uriComponentsBuilder.build().encode().toUriString();
        System.out.println(uriString);

        LinkedList<String> strings = new LinkedList<String>();
        strings.add("4");
        JSONForBook jsonForBook = new JSONForBook("O pejskovi", "0", strings);

        URI location = getRestTemplate().postForLocation(uriString, jsonForBook);
        System.out.println(location);
    }

    private void testGetBookWithWithPublisherId5() {
        String uri = AP;
        uri += "books/publisher/5";

        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
        final String uriString = uriComponentsBuilder.build().encode().toUriString();
        System.out.println(uriString);
        String response = getRestTemplate().getForObject(uriString, String.class);
        System.out.println(response);
    }

    private void testGetAllBooks() {
        String uri = AP;
        uri += "books";

        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
        final String uriString = uriComponentsBuilder.build().encode().toUriString();
        System.out.println(uriString);
        String response = getRestTemplate().getForObject(uriString, String.class);
        System.out.println(response);
    }

    private void testGetBookWithId6() {
        String uri = AP;
        uri += "books/6";

        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
        final String uriString = uriComponentsBuilder.build().encode().toUriString();
        System.out.println(uriString);
        String response = getRestTemplate().getForObject(uriString, String.class);
        System.out.println(response);
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class JSONForBook {

        @JsonProperty("name")
        private String name;

        @JsonProperty("authorId")
        private String authorId;

        @JsonProperty("publishersId")
        private List<String> publishersId;

        public JSONForBook(String name, String authorId, List<String> publishersId) {
            this.name = name;
            this.authorId = authorId;
            this.publishersId = publishersId;
        }
    }

}
